﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace zmMoniter
{
    public partial class frmArtificialUserList : DevExpress.XtraEditors.XtraForm
    {
        private mainForm main = null;
        private int inRow = -1;
        public frmArtificialUserList(mainForm frm)
        {
            InitializeComponent();
            main = frm;
        }

        private void sBtn_Add_Click(object sender, EventArgs e)
        {
           
            main.showArtificialUser(true);
        }

        private void sBtn_Edit_Click(object sender, EventArgs e)
        {
            if (inRow < 0)
                return;
            int iID = int.Parse(gridView1.GetRowCellValue(inRow, "编号").ToString());
            string sRFID = gridView1.GetRowCellValue(inRow, "RFID").ToString();
            string sName = gridView1.GetRowCellValue(inRow, "姓名").ToString();
            string sPos = gridView1.GetRowCellValue(inRow, "负责部位").ToString();
            string sTel = gridView1.GetRowCellValue(inRow, "联系方式").ToString();
            string sDepart= gridView1.GetRowCellValue(inRow, "所在部门").ToString();
            main.showArtificialUser(true, iID, sRFID, sName, sPos, sTel, sDepart);
        }

        private void sBtn_Delete_Click(object sender, EventArgs e)
        {
            //取得选定行信息  
            if (inRow < 0)
                return;
            string sID = gridView1.GetRowCellValue(inRow, "编号").ToString();
            string sUserName = gridView1.GetRowCellValue(inRow, "姓名").ToString();
            DialogResult bDelete = MessageBox.Show("您确认需要删除用户：" + sUserName + "?", "删除人工振捣用户提示",
                   MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (bDelete == DialogResult.Yes)
            {
                if (deleteUser(sID))
                {
                    UpdateDataList();
                }
            }
        }

        private void frmArtificialUser_Load(object sender, EventArgs e)
        {
            UpdateDataList();
            
        }
        public bool UpdateDataList()
        {
            bool bRst = false;
            
            string sSQL = "select 人工振捣基本信息表.编号,人工振捣基本信息表.RFID,人工振捣基本信息表.姓名,人工振捣基本信息表.联系方式,人工振捣基本信息表.负责部位," +
            "(case when 人工振捣基本信息表.是否在线=1 then '在线' else '离线' end) 是否在线,人工振捣基本信息表.所在部门," +
            "to_char(人工振捣基本信息表.创建时间,'YYYY/MM/DD HH24:MI:SS') AS 创建时间," +
            "仓面信息.仓面名称 AS 仓面名称 " +
            "FROM 人工振捣基本信息表 LEFT OUTER JOIN " +
            "仓面信息 ON 人工振捣基本信息表.仓面号 = 仓面信息.编号";

            sSQL = "select rownum AS 序号,t.* from(" + sSQL + ") t";
            PublicDataCls db = new PublicDataCls();
            string sSQLState = db.OpenConn();
            if (sSQLState != "")
            {
                this.gridCtl_ArtificialUser.DataSource = null;

                return bRst;
            }
            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();
            if (dt == null)
            {
                this.gridCtl_ArtificialUser.DataSource = null;
                return bRst;
            }
            this.gridCtl_ArtificialUser.DataSource = dt;
            bRst = true;
            inRow = -1;
            sBtn_Edit.Enabled = false;
            sBtn_Delete.Enabled = false;
            return bRst;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            int iRow = e.RowHandle;
            if (iRow < 0)
                return;
            inRow = iRow;
            sBtn_Edit.Enabled = true;
            sBtn_Delete.Enabled = true;
        }
        private bool deleteUser(string sID)
        {
            bool bResult = false;

            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "Delete 人工振捣基本信息表 WHERE 编号=" + sID;
            int iResult = db.ExecuteNonQuery(sSQL);
            if (iResult > 0)
            {
                bResult = true;
            }

            return bResult;
        }
    }
}